package com.tm.controller;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageRequest;
import com.aliyuncs.imageaudit.model.v20191230.ScanImageResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.tm.imgenum.ThImgEnum;
import com.tm.pojo.ImageDynamic;
import com.tm.service.ImageFilterService;
import com.tm.util.AliOSSUtil;
import com.tm.util.AliyunImgUtil;
import com.tm.util.EmailNotofication;
import jdk.nashorn.internal.ir.Flags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.ImageFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @create 2022-09-18 17:16
 */
@RestController
@RequestMapping("image")
public class ImageFilterController {
    @Autowired
    private ImageFilterService imageFilterService;

    private static String ALIYUN_ACCESS_KEY_ID = "LTAI5tGAfCbyfRga7ui1U9Xi";

    private static String ALIYUN_ACCESS_KEY_SECRET = "QQQAljx8pZJ42hinBAdK4EGWyBGsbR";

    private static String REGION_ID = "cn-shanghai";



    //阿里云oss实现存储文件
    @PostMapping("uploadFile")
    public String uploadFile(@RequestBody MultipartFile zkjFile,ImageDynamic imageDynamic) throws IOException {

        String upload = AliOSSUtil.upload(zkjFile);

        Integer id = imageDynamic.getId();

        DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ALIYUN_ACCESS_KEY_ID, ALIYUN_ACCESS_KEY_SECRET);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/

        IAcsClient client = new DefaultAcsClient(profile);

        ScanImageRequest request = new ScanImageRequest();

        List<ScanImageRequest.Task> taskList = new ArrayList<>();

        ScanImageRequest.Task task1 = new ScanImageRequest.Task();


        task1.setImageURL(upload);
        task1.setDataId("67");
        taskList.add(task1);
        request.setTasks(taskList);

        List<String> sceneList = new ArrayList<>();
        sceneList.add("porn");
        sceneList.add("terrorism");
        sceneList.add("ad");
        sceneList.add("live");
        sceneList.add("logo");
        request.setScenes(sceneList);

        try {
            ScanImageResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            String s = new Gson().toJson(response);
            ScanImageResponse.Data data = response.getData();
            List<ScanImageResponse.Data.Result> results = data.getResults();
            ScanImageResponse.Data.Result result = results.get(0);
            List<ScanImageResponse.Data.Result.SubResult> subResults = result.getSubResults();
            StringBuffer flags = new StringBuffer();
            subResults.forEach(a-> {
                if(!a.getSuggestion().equals("pass")){
                    String scene = a.getScene();
                    switch (scene) {
                        case "porn":
                            flags.append(ThImgEnum.PORN.getCode());
                            break;
                        case "ad":
                            flags.append(ThImgEnum.AD.getCode());
                            break;
                        case "terrorism":
                            flags.append(ThImgEnum.TERRORISM.getCode());
                            break;
                        case "logo":
                            flags.append(ThImgEnum.LOGO.getCode());
                            break;
                        case "live":
                            flags.append(ThImgEnum.LIVE.getCode());
                            break;
                        default:
                            break;
                    }
                }




            });
            //flags就有了所有结果
            //12345
            System.out.println(flags);
            //将机器的flags存入数据库中
            //将人工的审核标志改为待审核

            if (flags.toString().equals("")){
                imageDynamic.setDynamicImgUrl(upload);
                imageDynamic.setDynamicImgPortraitFlag(0);
                imageDynamic.setDynamicImgPortraitRflag(0);
                imageDynamic.setReason(0);
                imageFilterService.addImgs(imageDynamic);
            }else {
                Integer flag = Integer.valueOf(String.valueOf(flags));
                imageDynamic.setDynamicImgUrl(upload);
                imageDynamic.setDynamicImgPortraitFlag(1);
                imageDynamic.setDynamicImgPortraitRflag(2);
                imageDynamic.setReason(flag);
                imageFilterService.addImg(imageDynamic);
            }


        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrorCode());
            System.out.println("ErrMsg:" + e.getErrorMessage());
            System.out.println("RequestId:" + e.getRequestId());
        } catch (com.aliyuncs.exceptions.ServerException e) {
            throw new RuntimeException(e);
        } catch (com.aliyuncs.exceptions.ClientException e) {
            throw new RuntimeException(e);
        }



        return "上传成功工作人员将在两个小时内审核完毕！";
    }


}
